/**
 * 
 */
package com.runningfox.humor;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.runningfox.model.Joke;

/**
 * @author luhaiyang d
 */
public class DetailLayerCtrl extends WebViewClient implements OnClickListener,
		 OnItemClickListener, OnTouchListener {
	Humor callback;
	public Joke curNode;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.webkit.WebViewClient#onLoadResource(android.webkit.WebView,
	 * java.lang.String)
	 * 
	 * 
	 */
	@Override
	public void onLoadResource(WebView view, String url) {
//		Log.v("loadres", url);
//		String imageURL = url;
//		if (url.startsWith("http:")) {
//			imageURL = callback.actionCacheImage(url);
//		}
//		if (imageURL != null) {
//			super.onLoadResource(view, imageURL);
//		} else {
//			super.onLoadResource(view, url);
//		}
		callback.actionShowLoading("加载图片");
		super.onLoadResource(view, url);
	}

	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.
	 * WebView, java.lang.String) url 协议： !share:文本内容 (启动分享流程，并将文本内容进行分享)
	 * !img:imageurl (浏览大图，弹出图片管理器) !email:xxx@xxx.com (给已经绑定邮件的好友发邮件)
	 * http://.... (正常的http链接，需要跳转浏览器)
	 * !view:id 加载
	 * !pre上一篇
	 * !next下一篇
	 * !comment添加评论
	 * !share分享
	 */
	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url) {
		if(url.startsWith("!img:")){
			String tmpURL = url.substring(5);
			String imageFileName = callback.actionCacheImage(tmpURL);
			callback.actoinShowImageLayer(imageFileName);
			return true;
		}else if(url.startsWith("!showmenu")){
			callback.actionShowMenu();
			return true;
		}else if(url.startsWith("http://")){
			Intent it = new Intent(Intent.ACTION_VIEW);       
			Uri uri = Uri.parse(url);       
			it.setDataAndType(uri, "text/html");
			callback.startActivity(it); 
			return true;
		}else if(url.startsWith("!pre")){
			callback.actionLoadPreJoke(curNode);
			return true;
		}else if(url.startsWith("!next")){
			callback.actionLoadNextJoke(curNode);
			return true;
		}else if(url.startsWith("!share")){
			callback.actionShareJoke(curNode);
			return true;
		}else if(url.startsWith("!comment")){
			callback.actionActiveCommentLayer();
			return true;
		}else if(url.startsWith("!like")){
			callback.actionLike(curNode);
			return true;
		}
		return super.shouldOverrideUrlLoading(view, url);
	}

	
	
	@Override
	public void onPageFinished(WebView view, String url) {
		// TODO Auto-generated method stub
		super.onPageFinished(view, url);
//		callback.actionFinishLoading();
		callback.actionFinishLoading();
	}

	
	@Override
	public void onPageStarted(WebView view, String url, Bitmap favicon) {
		// TODO Auto-generated method stub
		super.onPageStarted(view, url, favicon);
//		callback.actionShowLoading(callback.getText(R.string.loading));
	}

	/**
	 * @param callback
	 */
	public DetailLayerCtrl(Humor callback) {
		super();
		this.callback = callback;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == callback.imgSend) {
			EditText edit = callback.editComment;
			String comment = edit.getText().toString();
			if (comment.length() > 0 && comment.length() <= 200) {
				callback.actionSendComment(this.curNode, comment);
			} else if (comment.length() > 200) {
				callback.actionShowTip(callback.getText(R.string.nomorechars));
			}
		} else if (v == callback.imgCommentClose) {
			callback.actionActiveDetailLayer();
		} else if(v==callback.txtBack||v==callback.txtBackTop){
			callback.actionActiveListLayer();
		}else if(v==callback.txtPre||v==callback.txtPreTop){
			callback.actionLoadPreJoke(curNode);
		}else if(v==callback.txtNext||v==callback.txtNextTop){
			callback.actionLoadNextJoke(curNode);			
		}
	}

	/*
	 * (non-Javadoc) comment list control
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
	 * .AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();
//		TextView cur =JokeNodeView.this.txtLike; 
		switch(action){
			case MotionEvent.ACTION_DOWN:{
				v.setBackgroundColor(Color.BLUE);
				break;
			}
			case MotionEvent.ACTION_UP:{
				v.setBackgroundColor(0);
				break;
			}default:{
				v.setBackgroundColor(0);
			}
		}
		return false;
	}

}
